package com.linln.modules.api.service.impl;

import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.StringUtil;
import com.linln.modules.api.domain.Project;
import com.linln.modules.api.repository.ProjectRepository;
import com.linln.modules.api.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author cocia
 * @date 2020/09/21
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectRepository projectRepository;
    public static int DATA_START_ROW = 1;//Excel导入初始行（excel中的实际行）


    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Project getById(Long id) {
        return projectRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Project> getPageList(Example<Project> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return projectRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param project 实体对象
     */
    @Override
    public Project save(Project project) {
        return projectRepository.save(project);
    }

//    @Override
//    public Project getByProjectCode(String code) {
//        List<Project> projects=projectRepository.findByProjCode(code);
//        if (projects.size()>0)
//            return projects.get(0);
//        else
//            return null;
//
//    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return projectRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    @Transactional
    public void match(List<Project> inProjects) {
        List<Project> saveProjects=new ArrayList<>();
        for (Project inProject:inProjects){
            List<Project> dbProjects=null;


            Project dbProject=null;
            if (dbProjects.size()>=1){
                dbProject=dbProjects.get(0);
                //fields表示需要复制的字段名称
                EntityBeanUtil.copyProperties(inProject,dbProject,
                        "jz","jsnr","jsdw","sjdwmc","byxcqk","bjdjd","jhkssj","jhjgsj");
                saveProjects.add(dbProject);
            }else {
                saveProjects.add(inProject);
            }
        }
        projectRepository.saveAll(saveProjects);
    }

    @Override
    public StringBuffer validProject(List<Project> projects) {
        StringBuffer msg=new StringBuffer();
        for (int i=0;i< projects.size();i++){
            String line="";
            Project project=projects.get(i);
            //案卷号不能为空

            if (!StringUtil.isEmpty(line.trim())) {
                line = "第" + (i + 1) + "行，" + line+"\n";
                msg.append(line);
            }
        }
        if (msg.length() > 0) {//存在错误
            msg.append("====导入失败，请修改后重试====\n");
        }
        return msg;
    }

    @Override
    public void exeImport(List<Project> inRegionGdps) {
        projectRepository.saveAll(inRegionGdps);
    }
}